package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SchoolClasses;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.SchoolClassesTj;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.system.service.ISchoolClassesService;

import javax.servlet.http.HttpServletResponse;

/**
 * 班级信息Controller
 * 
 * @author ruoyi
 * @date 2022-11-19
 */
@RestController
@RequestMapping("/classes")
public class SchoolClassesController extends BaseController
{
    @Autowired
    private ISchoolClassesService schoolClassesService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询班级信息列表
     */
    @RequiresPermissions("business:classes:list")
    @GetMapping("/list")
    public TableDataInfo list(SchoolClasses schoolClasses)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            schoolClasses.setSchoolId(user.getDeptId());
        }
        startPage();
        List<SchoolClasses> list = schoolClassesService.selectSchoolClassesList(schoolClasses);
        return getDataTable(list);
    }

    @GetMapping("/listTj")
    public TableDataInfo listTj(SchoolClasses schoolClasses)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            schoolClasses.setSchoolId(user.getDeptId());
        }
        startPage();
        List<SchoolClassesTj> list = schoolClassesService.selectSchoolClassesTjList(schoolClasses);
        return getDataTable(list);
    }

    @GetMapping("/listTjHasClass")
    public TableDataInfo listTjHasClass(SchoolClasses schoolClasses)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            schoolClasses.setSchoolId(user.getDeptId());
        }
        startPage();
        List<SchoolClassesTj> list = schoolClassesService.selectSchoolClassesHasList(schoolClasses);
        return getDataTable(list);
    }

    @GetMapping("/listTjNoClass")
    public TableDataInfo listTjNoClass(SchoolClasses schoolClasses)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            schoolClasses.setSchoolId(user.getDeptId());
        }
        startPage();
        List<SchoolClassesTj> list = schoolClassesService.selectSchoolClassesNoList(schoolClasses);
        return getDataTable(list);
    }

    /**
     * 导出班级信息列表
     */
    @RequiresPermissions("business:classes:export")
    @Log(title = "班级信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, SchoolClasses schoolClasses)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            schoolClasses.setSchoolId(user.getDeptId());
        }
        List<SchoolClasses> list = schoolClassesService.selectSchoolClassesList(schoolClasses);
        for (SchoolClasses obj:list) {
            SysDept sysDept = sysDeptService.selectDeptById(obj.getSchoolId());
            SysUser sysUserJw = sysUserService.selectUserById(obj.getJwteacherId());
            SysUser sysUserJx = sysUserService.selectUserById(obj.getJxteacherId());
            SysUser sysUserOp = sysUserService.selectUserById(obj.getUserId());
            obj.setSchoolName(sysDept.getDeptName());
            obj.setJwusername(sysUserJw.getUserName());
            obj.setJwphone(sysUserJw.getPhonenumber());
            obj.setJxusername(sysUserJx.getUserName());
            obj.setJxphone(sysUserJx.getPhonenumber());
            obj.setUsername(sysUserOp.getUserName());
            obj.setPhone(sysUserOp.getPhonenumber());
        }
        ExcelUtil<SchoolClasses> util = new ExcelUtil<SchoolClasses>(SchoolClasses.class);
        util.exportExcel(response, list, "班级信息");
    }

    /**
     * 获取班级信息详细信息
     */
    @RequiresPermissions("business:classes:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(schoolClassesService.selectSchoolClassesById(id));
    }

    @RequiresPermissions("business:classes:queryxs")
    @GetMapping(value = "/xs/{id}")
    public AjaxResult getInfoxs(@PathVariable("id") Long id)
    {
        return AjaxResult.success(schoolClassesService.selectSchoolClassesXsById(id));
    }

    /**
     * 新增班级信息
     */
    @RequiresPermissions("business:classes:add")
    @Log(title = "班级信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SchoolClasses schoolClasses)
    {
        return toAjax(schoolClassesService.insertSchoolClasses(schoolClasses));
    }

    /**
     * 修改班级信息
     */
    @RequiresPermissions("business:classes:edit")
    @Log(title = "班级信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SchoolClasses schoolClasses)
    {
        return toAjax(schoolClassesService.updateSchoolClasses(schoolClasses));
    }

    /**
     * 删除班级信息
     */
    @RequiresPermissions("business:classes:remove")
    @Log(title = "班级信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        schoolClassesService.checkCanDelete(ids);
        return toAjax(schoolClassesService.deleteSchoolClassesByIds(ids));
    }
}
